﻿
@{
    ViewBag.Title = "基础数据管理";
}
<style type="text/css">
    .checkbox2 {
        border: 1px solid #b2b2b2;
        box-sizing: border-box;
        height: 20px;
        width: 20px;
    }
</style>

<div class="title-content">基础数据管理</div>
<div class="row">
    @*<div class="col-md-6">
        <div class="portlet box red">
            <div class="portlet-title">
                <div class="caption">
                    <i class="fa fa-cogs"></i>课程
                </div>
                <div class="tools">
                    <a href="javascript:;" class="collapse" data-original-title="" title=""> </a>
                </div>
            </div>
            <div class="portlet-body" style="display: block;">
                <div class="btn-group btn-group-xs btn-group-solid">
                    <button class="btn blue" data-toggle="modal" href="#addNew" onclick="AddData('course')">新增</button>
                    <button class="btn red" data-toggle="modal" style="margin-left:15px; " onclick="DelData('chkCourse')">删除</button>
                </div>
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th><input type="checkbox" id="ckCourseAll" class="checkbox2" style="-webkit-appearance:checkbox;opacity:1;" /></th>
                                <th>#</th>
                                <th>课程名称</th>
                            </tr>
                        </thead>
                        <tbody id="courseTBody"></tbody>
                    </table>
                    <script id="courseHtml" type="text/html">
                        <% for(var i=0;i
                        <courseList.length;i++){%>
                            <% var item=courseList[i] %>
                            <tr>
                                <td><input type="checkbox" name="chkCourse" class="checkbox2" value="<%=item.id %>" style="-webkit-appearance:checkbox;opacity:1;margin-left:3px;" /></td>
                                <td><%=i+1 %></td>
                                <td><%=item.name %></td>
                            </tr>
                            <%}%>
                    </script>
                </div>
            </div>
        </div>
    </div>*@
    <div class="col-md-6">
        <div class="portlet box blue">
            <div class="portlet-title">
                <div class="caption">
                    <i class="fa fa-cogs"></i>餐类型
                </div>
                <div class="tools">
                    <a href="javascript:;" class="collapse" data-original-title="" title=""> </a>
                </div>
            </div>
            <div class="portlet-body" style="display: block;">
                <div class="btn-group btn-group-xs btn-group-solid">
                    <button class="btn blue" data-toggle="modal" href="#addNew" onclick="AddData('recipe_type')">新增</button>
                    <button class="btn red" data-toggle="modal" style="margin-left:15px;" onclick="DelData('chkRipiceType')">删除</button>
                </div>
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th><input type="checkbox" id="chkAllType" class="checkbox2" style="-webkit-appearance:checkbox;opacity:1;" /></th>
                                <th> # </th>
                                <th> 类型名称 </th>
                            </tr>
                        </thead>
                        <tbody id="canTbody"></tbody>
                    </table>
                    <script id="canTypeHtml" type="text/html">
                        <% for(var i=0;i
                        <data.length;i++){%>
                            <% var item=data[i] %>
                            <tr>
                                <td><input type="checkbox" name="chkRipiceType" class="checkbox2" value="<%=item.id %>" style="-webkit-appearance:checkbox;opacity:1;margin-left:3px;" /></td>
                                <td><%=i+1 %></td>
                                <td><%=item.name %></td>
                            </tr>
                            <%}%>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
@*<div class="row">
    <div class="col-md-6">
        <div class="portlet box green">
            <div class="portlet-title">
                <div class="caption">
                    <i class="fa fa-cogs"></i>食谱
                </div>
                <div class="tools">
                    <a href="javascript:;" class="collapse" data-original-title="" title=""> </a>
                </div>
            </div>
            <div class="portlet-body" style="display: block;">
                <div class="btn-group btn-group-xs btn-group-solid">
                    <button class="btn blue" data-toggle="modal" href="#addNew" onclick="AddData('recipe')">新增</button>
                    <button class="btn red" data-toggle="modal" style="margin-left:15px;" onclick="DelData('chkCai')">删除</button>
                </div>
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th><input type="checkbox" id="chkCaiAll" class="checkbox2" style="-webkit-appearance:checkbox;opacity:1;" /></th>
                                <th> # </th>
                                <th> 菜名 </th>
                            </tr>
                        </thead>
                        <tbody id="repicetbody"></tbody>
                    </table>
                    <script id="ricepiceHtml" type="text/html">
                        <% for(var i=0;i
                        <repiceList.length;i++){%>
                            <% var item=repiceList[i] %>
                            <tr>
                                <td><input type="checkbox" name="chkCai" class="checkbox2" value="<%=item.id %>" style="-webkit-appearance:checkbox;opacity:1;margin-left:3px;" /></td>
                                <td><%=i+1 %></td>
                                <td><%=item.name %></td>
                            </tr>
                            <%}%>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>*@

<!-- 弹出层，新增-->
<div id="addNew" class="modal fade" tabindex="-1" data-focus-on="input:first">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
            <h4 class="modal-title">新增<span class="names"></span></h4>
        </div>
        <div class="modal-body">
            <div style="margin-left:10px;">
                <div action="#" class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="inputEmail1" class="col-md-4 control-label"><span class="names"></span>名称</label>
                        <div class="col-md-6">
                            <input type="text" class="form-control" id="dNames" placeholder="">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button class="btn default" data-dismiss="modal" id="btnClose" aria-hidden="true">关闭</button>
            <button class="btn green" data-dismiss="" onclick="UpdateData()">提交</button>
        </div>
    </div>
</div>




<script type="text/javascript">
    //加载
    $(document).ready(function () {
        //查询基础数据
        getDatas();
        //课程checkbox
        //$("#ckCourseAll").click(function () {
        //    var checked = this.checked;
        //    var count = $("input[name='chkCourse']:checked").length;//选中
        //    var total = $("input[name='chkCourse']").length;//全部个数
        //    if (checked && count == total) {
        //        return;
        //    }
        //    if (count < total) {//全选
        //        $("input[name='chkCourse']").each(function (index, element) {
        //            var itemCk = $(element)[0].checked;
        //            if (itemCk == false) {
        //                $(element).click();
        //            }
        //        });
        //    } else {//取消全选
        //        $("input[name='chkCourse']").each(function (index, element) {
        //            $(element).click();
        //        });
        //    }
        //});

        //餐类型checkbox
        $("#chkAllType").click(function () {
            var checked = this.checked;
            var count = $("input[name='chkRipiceType']:checked").length;//选中
            var total = $("input[name='chkRipiceType']").length;//全部个数
            if (checked && count == total) {
                return;
            }
            if (count < total) {//全选
                $("input[name='chkRipiceType']").each(function (index, element) {
                    var itemCk = $(element)[0].checked;
                    if (itemCk == false) {
                        $(element).click();
                    }
                });
            } else {//取消全选
                $("input[name='chkRipiceType']").each(function (index, element) {
                    $(element).click();
                });
            }
        });

        //食谱checkbox
        //$("#chkCaiAll").click(function () {
        //    var checked = this.checked;
        //    var count = $("input[name='chkCai']:checked").length;//选中
        //    var total = $("input[name='chkCai']").length;//全部个数
        //    if (checked && count == total) {
        //        return;
        //    }
        //    if (count < total) {//全选
        //        $("input[name='chkCai']").each(function (index, element) {
        //            var itemCk = $(element)[0].checked;
        //            if (itemCk == false) {
        //                $(element).click();
        //            }
        //        });
        //    } else {//取消全选
        //        $("input[name='chkCai']").each(function (index, element) {
        //            $(element).click();
        //        });
        //    }
        //});
    });

    //查询基础数据
    function getDatas() {
        //查询基础数据
        AjaxCustom({
            url: "/BaseData/GetData",
            type: "get",
            showLoading: true,//是否显示遮罩层
            //parames: {},
            callBack: function (obj) {
                ////课程
                //if (obj.data.courseList.length == 0) {
                //    $("#courseTBody").html("<tr><td colspan=\"3\" style='text-align:center;'><b style='color:red;'>暂无数据</b></td></tr>");
                //} else {
                //    var courseHtml = template('courseHtml', obj.data);
                //    $("#courseTBody").html(courseHtml);
                //}
                //餐类型
                if (obj.length == 0) {
                    $("#canTbody").html("<tr><td colspan=\"3\" style='text-align:center;'><b style='color:red;'>暂无数据</b></td></tr>");
                } else {
                    var canTypeHtml = template('canTypeHtml', obj);
                    $("#canTbody").html(canTypeHtml);
                }
                ////食谱
                //if (obj.data.repiceList.length == 0) {
                //    $("#repicetbody").html("<tr><td colspan=\"3\" style='text-align:center;'><b style='color:red;'>暂无数据</b></td></tr>");
                //} else {
                //    var ricepiceHtml = template('ricepiceHtml', obj.data);
                //    $("#repicetbody").html(ricepiceHtml);
                //}
            }
        });
    }

    //类型，名称，id，操作类型
    var Type = "course", Name, ID, Action

    //新增
    function AddData(type) {
        //更改类型
        Action = "Add";
        Name = "";
        Type = type;
        $("#dNames").val("");
        switch (type) {
            case "course":
                $(".names").html("课程");
                break;
            case "recipe":
                $(".names").html("食谱");
                break;
            case "recipe_type":
                $(".names").html("餐类型");
                break;
        }
    }

    //删除
    function DelData(type) {
        ID = "";//清空id
        var chk = document.getElementsByName(type + "");
        for (var i = 0; i < chk.length; i++) {
            if (chk[i].checked) {
                ID += chk[i].value + "#";
            }
        }
        if (ID == "") {
            AlertMsg("没有选择数据！");
            return;
        }
        if (confirm("你确定要删除选中项吗？")) {
            Action = "Del";
            //执行删除
            UpdateData();
        }
    }

    //修改或删除
    function UpdateData() {
        //名称
        Name = $("#dNames").val();
        AjaxCustom({
            url: "/BaseData/UpdateData",
            type: "get",
            showLoading: true,
            parames: { id: ID, name: Name, type: Type, action: Action },
            callBack: function (obj) {
                $("#btnClose").click();//隐藏
                if (obj.code == "-2") {
                    AlertMsg("数据已存在！");
                }
                else if(parseInt(obj.code)>0) {
                    AlertMsg("操作成功！");
                    getDatas();
                }
                else if (parseInt(obj.code) < 1 && obj.code == "-2") {
                    AlertMsg("操作失败！");
                }
            }
        });
    }

</script>
